package hk.m4s.lr.repair.test.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.MaintenancekModel;
import hk.m4s.lr.repair.test.model.PushModel;
import hk.m4s.lr.repair.test.model.projectModle;

/**
 * @author
 */
public class RepairWeiPartslAdapter extends BaseAdapter {
    private Context context;
    public List<MaintenancekModel.ProjectBean> items;
    LayoutInflater inflater;
    // /** volley 图像加载器 */
    // private ImageLoader imageLoader;

    public RepairWeiPartslAdapter(Context context, List<MaintenancekModel.ProjectBean> items) {
        this.context = context;
        this.items = items;
        inflater = LayoutInflater.from(context);
        // imageLoader = RequestManager.getImageLoader();
    }

    public int getCount() {
        return items.size();
    }

    public Object getItem(int position) {
        return getView(position, null, null);
    }

    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.grid_item_weixiu_repair_parts, parent, false);
            holder.push_name = (TextView) convertView.findViewById(R.id.push_name);
            holder.push_on = (Button) convertView.findViewById(R.id.push_on);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.push_on.setTag(position);
        final MaintenancekModel.ProjectBean item = items.get(position);
        // 设置维修项目名称
        holder.push_name.setText(item.getName());
        // 设置是否选中

        if (item.getState().equals("1")) {
            holder.push_on.setBackgroundResource(R.mipmap.set_ico_yes);
        } else {
            holder.push_on.setBackgroundResource(R.mipmap.set_ico_no);
        }
        holder.push_on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = (Integer) v.getTag();
                MaintenancekModel.ProjectBean deviceModel = items.get(num);
                if (deviceModel.getState().equals("1")) {
                    deviceModel.setState("-1");
                } else {
                    deviceModel.setState("1");
                }

                items.set(num, deviceModel);
                notifyDataSetChanged();

            }
        });
        return convertView;
    }

    public static class ViewHolder {
        TextView push_name;
        Button push_on;
    }
}
